public class linked_list_cycle_ii {
    //快慢指针
    //与环形链表一解法类似，只是多了一个步骤
    public ListNode detectCycle(ListNode head) {
        if(head==null||head.next==null||head.next.next==null) return null;
        ListNode fast = head.next.next, slow = head.next;
        while (slow!=fast) {
            if (fast == null || fast.next == null) return null;
            fast = fast.next.next;
            slow = slow.next;
        }
        //相遇后,快指针返回链表头，快慢指针步长相同，再次相遇，返回任意指针即为所得
        fast = head;
        while (slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return fast;
    }
}
